package com.honghe.controller;

import com.honghe.dto.PatientDTO;
import com.honghe.dto.PatientLoginDTO;
import com.honghe.dto.VisitDTO;
import com.honghe.entity.Patient;
import com.honghe.properties.JwtProperties;
import com.honghe.result.Result;
import com.honghe.service.PatientService;
import com.honghe.utils.JwtUtil;
import com.honghe.vo.DoctorVO;
import com.honghe.vo.PatOrderVO;
import com.honghe.vo.PatVisitVO;
import com.honghe.vo.PatientLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/patient")
@Tag(name ="病人信息接口")
public class PatientController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/add")
    @Operation(summary = "病人信息注册")
    public Result<String> addPatient(@RequestBody PatientDTO patientDTO){
        patientService.addPatient(patientDTO);
        return Result.success();
    }
    @PostMapping("/login")
    @Operation(summary = "病人信息登录")
    public Result<PatientLoginVO> login(@RequestBody PatientLoginDTO patientLoginDTO){
        Patient patient = patientService.login(patientLoginDTO);
        Map<String,Object> claims = new HashMap<>();
        claims.put("patId",patient.getId());
        String sign = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        PatientLoginVO patientLoginVO = new PatientLoginVO(
                patient.getId(),
                patient.getPhone(),
                patient.getName(),
                sign
        );
        return Result.success(patientLoginVO);
    }
    @PutMapping("/update")
    @Operation(summary = "病人信息更新")
    public Result<String> updatePatient(@RequestBody PatientDTO patientDTO){
        patientService.update(patientDTO);
        return Result.success();
    }
    @PostMapping("/visit")
    @Operation(summary = "病人就诊")
    @CacheEvict(cacheNames = "myVisit",key = "#visitDTO.patId")
    public Result<PatOrderVO> visitDoctor(@RequestBody VisitDTO visitDTO){
        PatOrderVO patOrderVO =patientService.visit(visitDTO);
        return Result.success(patOrderVO);
    }
    @GetMapping("/getDoctor")
    @Operation(summary = "通过部门id获取医生信息")
    @Cacheable(cacheNames = "doctor",key = "#deptId")
    public Result<List<DoctorVO>> getAllDoctor(Long deptId){
        List<DoctorVO> doctorVOS = patientService.getByDeptId(deptId);
        return Result.success(doctorVOS);
    }
    @GetMapping("/getDept")
    @Operation(summary = "获取所有部门信息及对应部门下的医生信息")
    @Cacheable(cacheNames = "deptWithDoc")//缓存所有部门信息及对应部门下的医生信息
    public Result<List<PatVisitVO>> getAllDept(){
        List<PatVisitVO> allDeptWithDoc = patientService.getAllDeptWithDoc();
        return Result.success(allDeptWithDoc);
    }
    @GetMapping("/getMyVisit/{patId}")
    @Operation(summary = "获取我的就诊记录")
    @Cacheable(cacheNames = "myVisit",key = "#patId")
    public Result<List<PatOrderVO>> getMyVisit(@PathVariable Long patId){
        List<PatOrderVO> patOrderVOS=patientService.getMyVisit(patId);
        return Result.success(patOrderVOS);
    }
    @PutMapping
    @Operation(summary = "举报医生未到")
    public Result<String> updateStatus(@RequestBody Long orderId){
        patientService.updateStatus(orderId);
        return Result.success("举报成功");
    }
}
